﻿@model DiscountModel
@inject AdminAreaSettings adminAreaSettings
@{
    var defaultGridPageSize = adminAreaSettings.DefaultGridPageSize;
    var gridPageSizes = adminAreaSettings.GridPageSizes;
}
<div class="panel-group">
    @await Component.InvokeAsync("AdminWidget", new { widgetZone = "admin_discount_details_history_top", additionalData = Model.Id })
    <div class="panel panel-default">
        <div class="panel-body">
            <div id="usagehistory-grid"></div>

            <script type="text/javascript">
                $(document).ready(function() {
                    $("#usagehistory-grid").kendoGrid({
                        dataSource: {
                            type: "json",
                            transport: {
                                read: {
                                    url: "@Html.Raw(Url.Action("UsageHistoryList", "Discount", new {discountId = Model.Id}))",
                                    type: "POST",
                                    dataType: "json",
                                    data: addAntiForgeryToken
                                },
                                destroy: {
                                    url: "@Html.Raw(Url.Action("UsageHistoryDelete", "Discount"))",
                                    type: "POST",
                                    dataType: "json",
                                    data: addAntiForgeryToken
                                }
                            },
                            schema: {
                                data: "Data",
                                total: "Total",
                                errors: "Errors",
                                model: {
                                    id: "Id",
                                    fields: {
                                        //do not implicitly specify all fields
                                        //we do it only for fields which implicitly require it
                                        //otherwise, they'll be formatted wrong way
                                        CreatedOn: { type: "date" }
                                    }
                                }
                            },
                            error: function(e) {
                                display_kendoui_grid_error(e);
                                // Cancel the changes
                                this.cancelChanges();
                            },
                            pageSize: @(defaultGridPageSize),
                            serverPaging: true,
                            serverFiltering: true,
                            serverSorting: true
                        },
                        pageable: {
                            refresh: true,
                            pageSizes: [@(gridPageSizes)],
                            @await Html.PartialAsync("_GridPagerMessages")
                        },
                        editable: {
                            confirmation: "@T("Admin.Common.DeleteConfirmation")",
                            mode: "inline"
                        },
                        scrollable: false,
                        columns: [
                        {
                            field: "CreatedOn",
                            title: "@T("Admin.Promotions.Discounts.History.CreatedOn")",
                            width: 200,
                            type: "date",
                            format: "{0:G}"
                        }, {
                            field: "CustomOrderNumber",
                            title: "@T("Admin.Promotions.Discounts.History.CustomOrderNumber")",
                            width: 200,
                            headerAttributes: { style: "text-align:center" },
                            attributes: { style: "text-align:center" },
                            template: '<a class="btn btn-default" href="@Url.Content("~/Admin/Order/Edit/")#=OrderId#"><i class="fa fa-eye"></i>#=CustomOrderNumber# - @T("Admin.Common.View")</a>'
                        }, {
                            field: "OrderTotal",
                            title: "@T("Admin.Promotions.Discounts.History.OrderTotal")",
                            width: 200
                        }, {
                            command: { name: "destroy", text: "@T("Admin.Common.Delete")" },
                            title: "@T("Admin.Common.Delete")",
                            width: 100
                        }
                        ]
                    });
                });
            </script>
        </div>
    </div>
    @await Component.InvokeAsync("AdminWidget", new { widgetZone = "admin_discount_details_history_bottom", additionalData = Model.Id })
</div>
